package com.java.yz.day11.ketang;

import java.util.Comparator;
import java.util.TreeSet;

/*
    比较器排序：指得是在创建TreeSet集合对象的时候，构造方法传入Comparator的对象，重写compare方法，来决定元素的去重和排序
    按照年龄从小到大排序
 */
public class TreeSetDemo3 {
    public static void main(String[] args) {
        //这是比较器的排序
        TreeSet<Teacher3> set = new TreeSet<Teacher3>(new Comparator<Teacher3>() {
            @Override
            public int compare(Teacher3 o1, Teacher3 o2) {
                //o1代表传进来的数据
                //o2代表树的根节点
                int i1 = o1.getAge()-o2.getAge();
                int i2 = (i1==0)?o1.getName().compareTo(o2.getName()):i1;
                return i2;
            }
        });
        set.add(new Teacher3("李白",40));
        set.add(new Teacher3("白居易",42));
        set.add(new Teacher3("杜甫",46));
        set.add(new Teacher3("李白",40));
        System.out.println(set);
    }
}
